﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{
	/// <summary>
	/// Interaction logic for Card.xaml
	/// </summary>
	public partial class Card : UserControl
	{
		public Card()
		{
			this.InitializeComponent();
            this.PreviewMouseDoubleClick += new MouseButtonEventHandler(Card_PreviewMouseDoubleClick);
		}        

        static Card()
        {            
            PresenterProperty = DependencyProperty.Register("Presenter", typeof(GamePresenter), typeof(Card), new PropertyMetadata(new PropertyChangedCallback(PresenterPropertyChanged)));
        }

        static DependencyProperty PresenterProperty;
        public GamePresenter Presenter
        {
            get
            {
                return (GamePresenter)base.GetValue(PresenterProperty);
            }
            set
            {
                base.SetValue(PresenterProperty, value);
            }
        }

        protected void PresenterPropertyChanged()
        {
            //mnuSendToZone.Items.Clear();

            //foreach (PlayerState player in Presenter.Model.Players)
            //{
            //    MenuItem item = new MenuItem();
            //    item.Header = player.Name;
            //    item.Click += new RoutedEventHandler(SendToPlayer_Click);
            //    mnuSendToZone.Items.Add(item);
            //}
        }        

        private static void PresenterPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            (o as Card).PresenterPropertyChanged();               
        }

        public Shared.GameCard GameCard
        {
            get
            {
                return (Shared.GameCard) DataContext;
            }
        }

		private void Card_Morph(object sender, System.Windows.RoutedEventArgs e)
		{
            if (GameCard.ControllerID != Presenter.Model.CurrentPlayer.PlayerID) { return; }
            GameCard.IsFaceDown = !GameCard.IsFaceDown;
		}

        void Card_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!(e.OriginalSource is Image))
            {
                e.Handled = true;
            }
        }

        private void Card_Tap(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.ToggleTap(GameCard);
        }
        
        private void Card_Counter(object sender, System.Windows.RoutedEventArgs e)
        {
            CounterEditor counterEditor = new CounterEditor();
            foreach (Shared.Counter counter in GameCard.Counters)
            {
                counterEditor.Counters.Add((Shared.Counter)counter.Clone());
            }

            if (counterEditor.ShowDialog() == true)
            {                
                GameCard.Counters = counterEditor.Counters;
            }
        }

        private void Card_Transfer(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.TransferControl(GameCard);
        }

        private void Card_Remove(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.RemoveCard(GameCard);
        }

        private void Card_ToHand(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.SendToHand(GameCard);
        }
        
        private void Card_ToDeckTop(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.SendToDeck(GameCard, Shared.DeckPosition.Top, 0);
        }

        private void Card_ToDeckBottom(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.SendToDeck(GameCard, Shared.DeckPosition.Bottom, 0);           
        }

        private void Card_ToDeckRandom(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.SendToDeck(GameCard, Shared.DeckPosition.Random, 0);
        }

        private void Card_ToGraveyard(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.SendToGraveyard(GameCard);
        }

        private void Card_ToWorkspace(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.SendToWorkspace(GameCard);
        }

        private void Card_ToDeckFromTop(object sender, System.Windows.RoutedEventArgs e)
        {
            string value = InputBox.ShowInput("How many places from the top should it be placed?", "0");
            int count;
            if (Int32.TryParse(value, out count))
            {
                Presenter.Model.SendToDeck(GameCard, Shared.DeckPosition.FromTop, count);
            }
            else
            {
                MessageBox.Show("Invalid number");
            }
        }

        private void Card_ToDeckFromBottom(object sender, System.Windows.RoutedEventArgs e)
        {
            string value = InputBox.ShowInput("How many places from the bottom should it be placed?", "0");
            int count;
            if (Int32.TryParse(value, out count))
            {
                Presenter.Model.SendToDeck(GameCard, Shared.DeckPosition.FromBottom, count);
            }
            else
            {
                MessageBox.Show("Invalid number");
            }
        }

        //void SendToPlayer_Click(object sender, RoutedEventArgs e)
        //{
        //    if (sender is MenuItem)
        //    {
        //        MenuItem item = sender as MenuItem;
        //        string playerName = item.Header.ToString();
        //        Shared.GameCard gameCard = (DataContext as Shared.GameCard);
        //        PlayerState toPlayer = Presenter.Model.PlayerByName(playerName);
        //        PlayerState fromPlayer = Presenter.Model.PlayerByID(gameCard.Location.PlayerID);

        //        if (toPlayer != fromPlayer)
        //        {
        //            Presenter.Model.MoveCard(fromPlayer, toPlayer, gameCard);
        //        }
        //    }
        //}

        private void btnToughnessIncrease_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.AddCounter(this.GameCard, Shared.CounterType.Toughness, "Toughness", 1);
        }

        private void btnToughnessDecrease_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.AddCounter(this.GameCard, Shared.CounterType.Toughness, "Toughness", -1);
        }

        private void btnPowerIncrease_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.AddCounter(this.GameCard, Shared.CounterType.Power, "Power", 1);
        }

        private void btnPowerDecrease_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Presenter.Model.AddCounter(this.GameCard, Shared.CounterType.Power, "Power", -1);
        }

        private void UserControl_Drop(object sender, System.Windows.DragEventArgs e)
        {
            Presenter.DropOnCard(this.GameCard, e);
        }
	}
}